package com.hyt.it.ogt.ks.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.utils.string.StringUtils;
import com.hyt.it.ogt.ks.enums.ApplyCheckSkipTypeCode;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.enums.log.ApplyCheckState;
import com.hyt.it.ogt.ks.enums.log.CandidateOperatorLogType;
import com.hyt.it.ogt.ks.enums.log.LogEventType;
import com.hyt.it.ogt.ks.exception.KsException;
import com.hyt.it.ogt.ks.model.entity.ApplyCheck;
import com.hyt.it.ogt.ks.model.vo.ApplyCheckVO;
import com.hyt.it.ogt.ks.model.vo.log.FaceApplyVO;
import com.hyt.it.ogt.ks.model.vo.monitor.response.WxLoginVO;
import com.hyt.it.ogt.ks.service.IApplyCheckService;
import com.hyt.it.ogt.ks.service.ILogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 跳过申请审核表 前端控制器
 * </p>
 *
 * @author liteng
 * @since 2022-11-30
 */
@RestController
@RequestMapping("/ks/apply-check")
@Api(tags = "24.申请跳过", value = "申请跳过")
@ApiSort(value = 24)
@Slf4j
public class ApplyCheckController {
    @Resource
    private IApplyCheckService iApplyCheckService;
    @Resource
    private ILogService iLogService;

    @ApiOperation(value = "24.1 申请跳过设备检测")
    @ApiOperationSort(1)
    @PutMapping("skip")
    public ApiResponse skip(@RequestBody ApplyCheckVO param) {
        try {
            String type = param.getType();
            int opType;
            if (ApplyCheckSkipTypeCode.ID_FACE.getCode().equals(type)) {
                opType = CandidateOperatorLogType.ID_FACE_SKIP_APPLY.getCode();
            } else if (ApplyCheckSkipTypeCode.DEVICE_CHECK.getCode().equals(type)) {
                opType = CandidateOperatorLogType.DEVICE_CHECK_SKIP_APPLY.getCode();
            } else if (ApplyCheckSkipTypeCode.MANUAL_REVIEW_FACE_APPLY.getCode().equals(type)) {
                opType = CandidateOperatorLogType.ID_FACE_SKIP_APPLY.getCode();
                FaceApplyVO manualReviewFaceApply = param.getManualReviewFaceApply();
                if (manualReviewFaceApply.getCandidatePhotos().size() == 0 || manualReviewFaceApply.getCertificatePhotos().size() == 0) {
                    return ApiResponse.<WxLoginVO>builder().code(BizStatusCode.MANUAL_REVIEW_FACE_APPLY.getCode()).build();
                }
            } else {
                log.error("类型错误：{}", type);
                throw new KsException(BizStatusCode.APPLY_CHECK_SKIP_FAIL.getCode(), BizStatusCode.APPLY_CHECK_SKIP_FAIL.getMsg());
            }
            iApplyCheckService.skipApply(param, opType, type);
        } catch (KsException e) {
            log.error("# 申请跳过业务异常：", e);
            return ApiResponse.<WxLoginVO>builder().code(e.getErrCode()).build();
        } catch (Exception e) {
            log.error("# 申请跳过异常：", e);
            return ApiResponse.<WxLoginVO>builder().code(BizStatusCode.APPLY_CHECK_SKIP_FAIL.getCode()).build();
        }
        return ApiResponse.<WxLoginVO>builder().code(BizStatusCode.KS_SUCCESS_OK.getCode()).build();
    }

    @ApiOperation(value = "24.2 获取跳过人脸验证信息")
    @ApiOperationSort(2)
    @GetMapping("getManualReviewFaceApplyInfo")
    public Object getManualReviewFaceApplyInfo(@RequestParam String examId, @RequestParam String candidateId) {
        try {
            log.info("获取跳过人脸验证信息请求参数：{}, {}", examId, candidateId);
            QueryWrapper<ApplyCheck> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApplyCheck::getExamId, examId)
                    .eq(ApplyCheck::getCandidateId,candidateId)
                    .eq(ApplyCheck::getEventType, LogEventType.OPERATOR_LOG.getCode())
                    .eq(ApplyCheck::getOperateType, CandidateOperatorLogType.ID_FACE_SKIP_APPLY.getCode())
                    .eq(ApplyCheck::getState, ApplyCheckState.WAIT_HANDLE.getCode())
                    .orderByDesc(ApplyCheck::getCreateDate)
                    .last("limit 1");
            ApplyCheck applyCheck = iApplyCheckService.getOne(queryWrapper);
            FaceApplyVO manualReviewFaceApply = new FaceApplyVO();
            if (applyCheck != null && StringUtils.isNotBlank(applyCheck.getOperateContent())) {
                manualReviewFaceApply = JSON.parseObject(applyCheck.getOperateContent(), FaceApplyVO.class);
            }
            return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode()).data(manualReviewFaceApply).build();
        } catch (Exception e) {
            log.error("# 获取跳过人脸验证信息异常：", e);
            return ApiResponse.builder().code(BizStatusCode.GET_MANUAL_REVIEW_FACE_APPLY_INFO_ERROR.getCode()).build();
        }
    }
}
